package com.yunhe.abnormal.controller.data;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.yunhe.abnormal.domain.data.AbnormalEventRecordMeas;
import com.yunhe.common.exception.ArgumentErrorException;
import com.yunhe.common.exception.ObjectNotFoundException;
import com.yunhe.common.exception.UniqueConstraintsException;
import com.yunhe.common.model.DomainList;
import com.yunhe.common.model.PageParam;
import com.yunhe.common.model.ResultObject;
import com.yunhe.abnormal.service.data.AbnormalEventRecordMeasService;
import com.yunhe.common.util.QueryUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

@Api(value = "abnormalEventRecordMeas",description = "异常事件记录量测维护", tags = {"Data_AbnormalEventRecordMeas"})
@RestController
@RequestMapping("/abnormalEventRecordMeas")
public class AbnormalEventRecordMeasController {
    @Autowired
    private AbnormalEventRecordMeasService abnormalEventRecordMeasService;

    @ApiOperation("新增事件")
    @RequestMapping(method = RequestMethod.POST)
    public ResponseEntity<ResultObject> post(@RequestBody AbnormalEventRecordMeas abnormalEventRecordMeas)
            throws ArgumentErrorException, UniqueConstraintsException, ObjectNotFoundException, JsonProcessingException {
        AbnormalEventRecordMeas result = abnormalEventRecordMeasService.create(abnormalEventRecordMeas);
        return new ResponseEntity<>(new ResultObject<>(result), HttpStatus.OK);
    }

    /**
     * 批量新增事件
     * @param domainList 事件对象列表
     */
    @ApiOperation("批量新增事件")
    @RequestMapping(value = "/batch", method = RequestMethod.POST)
    public ResponseEntity<ResultObject> batchPost(@RequestBody DomainList<AbnormalEventRecordMeas> domainList)
            throws Exception {
        List<AbnormalEventRecordMeas> eventList = domainList.getList();
        Iterable<AbnormalEventRecordMeas> results = abnormalEventRecordMeasService.create(eventList);
        return new ResponseEntity<>(new ResultObject<>(results), HttpStatus.OK);
    }

    /**
     * 根据事件ID删除事件
     * @param id 事件ID
     */
    @ApiOperation("根据事件ID删除事件")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "事件ID", required = true, dataType = "long", paramType = "path")
    })
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public ResponseEntity<ResultObject> delete(@PathVariable("id") Long id) {
        abnormalEventRecordMeasService.delete(id);
        return new ResponseEntity<>(new ResultObject<>(null), HttpStatus.OK);
    }

    @ApiOperation("根据条件获得事件列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pointNumbers", value = "告警量测点号值", dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "recordId", value = "事件记录ID", dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "dtime", value = "时间范围", dataType = "date", paramType = "query")
    })
    @RequestMapping(method = RequestMethod.GET)
    public ResponseEntity<ResultObject> findByCondition(@RequestParam("pointNumbers") Optional<List<Long>> pointNumbers,
                                                        @RequestParam("recordId") Optional<Long> recordId,
                                                        @ModelAttribute PageParam pageParam) {
        Iterable<AbnormalEventRecordMeas> results = abnormalEventRecordMeasService.retrieve(recordId, pointNumbers, pageParam);
        if (results instanceof Page) {
            Page<AbnormalEventRecordMeas> pageContent = (Page<AbnormalEventRecordMeas>) results;
            List<AbnormalEventRecordMeas> content = pageContent.getContent();
            long totalElements = pageContent.getTotalElements();
            int totalPages = pageContent.getTotalPages();
            int page = QueryUtil.getPage(pageParam) + 1;
            int size = QueryUtil.getSize(pageParam);
            return new ResponseEntity<>(new ResultObject<>(content, page, size, totalElements, totalPages), HttpStatus.OK);
        } else {
            return new ResponseEntity<>(new ResultObject<>(results), HttpStatus.OK);
        }
    }
}
